package com.juxiange.admin.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.juxiange.admin.entity.User;
import com.juxiange.admin.service.UserService;
import com.juxiange.common.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    /*
     * 用户信息修改
     * */
    @PutMapping  ("/update")  //(http://localhost:8081/admin/update)
    public R<String> update(HttpServletRequest request, @RequestBody User user){
        log.info(user.toString());   //测试是哪个user被传进来了
        //Long empId = (Long) request.getSession().getAttribute("employee");  //应该是获得用户输入
        user.setUpdateTime(LocalDateTime.now()); //修改时间
        user.setUpdateUser(2L);   //修改人是2号
        userService.updateById(user);     //更新修改

        return R.success("员工信息修改成功");
    }

    /*
     * 用户信息分页查询
     * */
    @GetMapping("/page")  //(http://localhost:8081/admin/page)
    public R<Page<User>> page(int page,int pageSize){
        log.info("page = {}, pageSize = {}",page,pageSize);  //参数接收

        //构造分页构造器（MP给的）
        Page<User> pageInfo = new Page<>(page,pageSize); //一页十条
        //构造条件构造器
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        //queryWrapper.like(StringUtils.isNotEmpty(name),User::getName,name);//判断了name不为空
        //添加排序条件
        queryWrapper.orderByDesc(User::getUpdateTime);
        //执行查询
        userService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }
}
